<?php


function getTasks($con, $db_name, $requestVars,$sessionEnabled,$type)
{
    $tid = $requestVars['tid'];
    $pid = $requestVars['pid'];
    $tlid = $requestVars['tlid'];    
    $status = $requestVars['status'];
    $all = $requestVars['all'];
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 
          
     
    $query = "SELECT t.* FROM $db_name.tasks t JOIN $db_name.tasks_assigned ta on t.id = ta.task WHERE ta.user = $uid";
    
    if($all)      
    {
        $query = "SELECT * FROM $db_name.tasks  ";        
    }

    if($pid)
    {
        if($all)
        {
            $query = $query. "WHERE  project = $pid";           
        }
        else
        {
            $query .= " AND project = $pid";
        }

    }

    if($tlid)
    {       
        if($pid || !$all)
        {
            $query .= " and liste = $tlid";
        }
        else 
        {
            $query .= "WHERE  liste = $tlid";
        }               

    }    
    
    if($tid)
    {
        $query = "SELECT * FROM $db_name.tasks Where ID = $tid";
    }

    if($status)
    {
        $query = $query." and status = $status";
    }

     $result = mysqli_query($con,$query);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$query]");
    }
    $TasksArray = array();
    $counter = 0;
    while($row = mysqli_fetch_assoc($result))
    {

     $id = $row['ID'];
     $pid = $row['project'];  
     $tlid = $row['liste'];     
     $start = $row['start'];
     $end = $row['end'];
     $name = $row['title'];
     $desc = $row['text'];    
     $status = $row['status'];

  

     $Task =  new Task($id, $pid, $tlid,$name,$desc,$start,$end,$status);

     $TasksArray[$counter] = $Task;
     $counter++;

    }
    
    if($tid)
    {
        if($type == 'json')
        {
            return TasksResponseJSON(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }
        else if($type == 'xml')
        {
            return TasksResponseXML(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }        
    }
    else
    {
        if($type == 'json')
        {
            return ArrayTasksResponseJSON(1,"Success!",$TasksArray);
        }
        else if($type == 'xml')
        {
            return ArrayTasksResponseXML(1,"Success!",$TasksArray);
        }        
    }       
   
         
     
}

function addTask($con, $db_name, $requestVars,$sessionEnabled, $type)
{
    $name = $requestVars['name'];
    $desc = $requestVars['desc'];
    $end = $requestVars['end'];

    $tlid =  $requestVars['tlid'];
    $pid = $requestVars['pid'];
    $auid = $requestVars['assign_uid']; // what user to assign this to?
    

    if(!$pid)
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(7, "You must pass a project id (pid) ! ");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(7, "You must pass a project id (pid) ! ");
        }       
        
    }
    
    if(!$tlid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(9, "You must pass a tasklist id (tlid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(9, "You must pass a tasklist id (tlid) !");
        }        
    }
    
   
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 
          
      
    if(!$auid ) // assign to the current user session.
    {
        $auid = $uid; // just the session userid. 
    }

    $end = mysqli_real_escape_string($con,$end);
    $name = mysqli_real_escape_string($con,$name);
    $desc = mysqli_real_escape_string($con,$desc);
    $tlid = (int) $tlid;
    $pid = (int) $pid;

    $end_fin = strtotime($end);

    if (empty($end_fin))
    {
        $end_fin = $end;
    }

    $start = time();
    // write to db
    $ins = mysqli_query($con,"INSERT INTO $db_name.tasks (start,end,title,text,liste,status,project) VALUES ('$start','$end_fin','$name','$desc',$tlid,1,$pid)");
    
    

    if ($ins)
    {
        
        $tid = mysqli_insert_id($con);
        
        $query  = "SELECT * From $db_name.tasks WHERE id = $tid";
        $result = mysqli_query($con,$query);

        if(!$result)
        {
            die("mysql error: ".mysql_error()."[$query]");
        }

        while($row = mysqli_fetch_assoc($result))
        {
            $id = $row['ID'];
            $pid = $row['project'];  
            $tlid = $row['liste'];     
            $start = $row['start'];
            $end = $row['end'];
            $name = $row['title'];
            $desc = $row['text'];    
            $status = $row['status'];

        }

        if($auid) // Are we assigning it to a user? (We are always inserting...)
        {
            // We need to assign the task and look we have the id of that task now :)                     
            //insert Assigned Task
            $ins = mysqli_query($con,"INSERT INTO $db_name.tasks_assigned (user, task) VALUES ('$auid','$insid')");                    

        }

        if($type == 'json')
        {            
            return TasksResponseJSON(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }
        else if($type == 'xml')
        {
            return TasksResponseXML(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }           
        
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(33,"Failed to create task, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(33,"Failed to create task, please try again later");
        }       
    }
        
    
}

function editTask($con, $db_name, $requestVars,$sessionEnabled,$type)
{
    $tid = $requestVars['tid'];
    
    if(!$tid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(10, "You must pass a task id (tid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(10, "You must pass a task id (tid) !");
        }        
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    } 
   // Let's add some smarts, and say the developer doesn't want to always grab and repass what is already there.
   // lets just update what is different. 
   $end = $requestVars['end'];
   $name = $requestVars['name'];
   $desc = $requestVars['desc'];
   $tlid = $requestVars['tlid'];
   $auid = $requestVars['assign_uid']; // what user to assign this to?

   if($auid == "me")
   {
       $userid = $uid;
   }

   $get ="select * from $db_name.tasks WHERE ID=$tid";
   $result =  mysqli_query($con,$get);

    if(!$result)
    {
        die("mysql error: ".mysql_error()."[$get]");
    }
    while($row = mysqli_fetch_assoc($result))
    {
        $id = $row['ID'];
        $start = $row['start'];
        $status = $row['status'];
        
        if(!$end)
        {
            $end = $row['end'];
        }
        else 
        {
           $end = mysqli_real_escape_string($con,$end);
          $end = strtotime($end);    
        }
        if(!$name)
        {                 
            $name = $row['title'];                    
        }

        if(!$desc)
        {
            $desc = $row['text'];
        }

        if(!$tlid)
        {                
            $tlid = $row['liste'];
        } 

    }         


    $name = mysqli_real_escape_string($con,$name);
    $desc = mysqli_real_escape_string($con,$desc);
    $tid = (int) $tid;
    
    $tlid = (int) $tlid;


    $upd = mysqli_query($con,"UPDATE $db_name.tasks SET `end`='$end',`title`='$name', `text`='$desc', `liste`=$tlid WHERE ID = $tid");
           // if it is always deleted on update than lets add the user...
    if($auid)
    {
        // I dont want to blindly delete the assigned task. 
        mysqli_query($con,"DELETE FROM $db_name.tasks_assigned WHERE `task` = $tid");

        $ins = mysqli_query($con,"INSERT INTO $db_name.tasks_assigned (user, task) VALUES ('$auid','$tid')");                    
    }

    if ($upd)
    {                
        if($type == 'json')
        {
            return TasksResponseJSON(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }
        else if($type == 'xml')
        {
            return TasksResponseXML(1,"Success!",$id, $pid, $tlid,$name,$desc,$start,$end,$status);
        }        
        
    }
    else
    {
        if($type == 'json')
        {            
            return GenericResponseJSON(16,"Failed to update tasklist, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(16,"Failed to update tasklist, please try again later");
        }           

    }  
}

function delTask($con, $db_name, $requestVars,$sessionEnabled, $type)
{
    $tid = $requestVars['tid'];
    
    if(!$tid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(10, "You must pass a task id (tid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(10, "You must pass a task id (tid) !");
        }        
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }  

    $tid = (int) $tid;

    $del = mysqli_query($con,"DELETE FROM $db_name.tasks WHERE ID = $tid LIMIT 1");
    if ($del)
    {
        $del2 = mysqli_query($con,"DELETE FROM $db_name.tasks_assigned WHERE task=$tid");
        
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(21, "Failed to delete task, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(21, "Failed to delete task, please try again later ");
        }
    }               
}

function openTask($con, $db_name, $requestVars,$sessionEnabled, $type)
{
     $tid = $requestVars['tid'];
    
    if(!$tid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(10, "You must pass a task id (tid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(10, "You must pass a task id (tid) !");
        }        
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }        
    $tid = (int) $tid;

    $upd = mysqli_query($con,"UPDATE tasks SET status = 1 WHERE ID = $tid");
    
    if ($upd) 
    {
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(29, "Failed to open task, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(29, "Failed to open task, please try again later");
        }
         
    }       
    
}

function closeTask($con, $db_name, $requestVars,$sessionEnabled)
{
     $tid = $requestVars['tid'];
    
    if(!$tid)
    { 
        if($type == 'json')
        {
            return GenericResponseJSON(10, "You must pass a task id (tid) !");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(10, "You must pass a task id (tid) !");
        }        
    }
    
   $requestVars =  CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, 'milestones','add',$type);
   
    if($requestVars['s_uid'] != "{" && $type == 'json' || 
       $requestVars['s_uid'] != "<" && $type == 'xml')
    {     
        $uid = $requestVars['s_uid'];        
    }
    else
    {  
        return $requestVars; // Because this is really the invalid response...
    }  
    $tid = (int) $tid;

    $upd = mysqli_query($con,"UPDATE tasks SET status = 0 WHERE ID = $tid");

    if ($upd) 
    {
        if($type == 'json')
        {
             return GenericResponseJSON(1, "Success!");
        }
        else if($type == 'xml')
        {
             return GenericResponseXML(1, "Success!");
        }       
    }
    else 
    {        
        if($type == 'json')
        {
            return GenericResponseJSON(25, "Failed to close task, please try again later");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(25, "Failed to close task, please try again later");
        }
         
    }        
}
?>
